package com.ruoyi.vqms.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.constant.WXConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.vqms.service.SendMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class SendMsgServiceImpl implements SendMsgService {

    @Autowired
    private RedisCache redisCache;

    @Override
    public void send_msg(String wxOpenId,
                         String reservationId,
                         String status,
                         String areaName,
                         String driverName,
                         String licensePlate) {
        String token = get_token();        //获取token
        if (token != null && !Objects.equals(token, "")) {
            send(token, wxOpenId, reservationId, status, areaName, driverName, licensePlate);
        }

    }

    public String get_token() {          //获取token
        String token = "";
        if (redisCache.getCacheObject("msg_token") == null) {  //如果token过期就重新获取
            System.out.println("token过期");
            String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
            tokenUrl = tokenUrl + "&appid=" + WXConstants.APPID + "&secret=" + WXConstants.SECRET;
            String result = HttpUtil.get(tokenUrl);
            JSONObject jsonObject = JSONUtil.parseObj(result);
            System.out.println("获取微信token返回==》" + jsonObject);
            token = jsonObject.get("access_token").toString();
            redisCache.setCacheObject("msg_token", token, 2, TimeUnit.HOURS);     //token存redis,两小时过期
        } else {
            System.out.println("重新获取");
            token = redisCache.getCacheObject("msg_token");
        }
        return token;
    }

    //发送排队预约消息
    public void send(String token, String wxOpenId, String reservationId, String status, String areaName, String driverName, String licensePlate) {       //发送消息
        String msgUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send";
        msgUrl = msgUrl + "?access_token=" + token;
        // 设置模板参数
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("touser", wxOpenId);                 // 接收方
        paramMap.put("template_id", WXConstants.TEMPLATEID_DRIVER);        // 模板id
        paramMap.put("page", "pages/index");         // 通知消息中点击”进入小程序查看“要跳转的路径
        // 设置data 模板内容
        HashMap<String, Object> data = new HashMap<>();
        data.put("character_string1", formatParam(reservationId));  //签到编号
        data.put("phrase2", formatParam(status));   //签到状态（默认签到）
        data.put("thing6", formatParam(areaName));       //月台编号
        data.put("thing8", formatParam(driverName));         //司机
        data.put("car_number10", formatParam(licensePlate));   //车牌号
        paramMap.put("data", data);
        // 转json字符串
        String jsonObject = JSONUtil.toJsonStr(paramMap);
        System.out.println("发送json-->" + jsonObject);
        String result = HttpUtil.post(msgUrl, jsonObject);
//        JSONObject object = new JSONObject(result);
//        int errcode = object.getInt("errcode");
        System.out.println("发送数据---》" + data);
        System.out.println("返回结果---》" + result);
    }

    public HashMap<String, Object> formatParam(String value) {
        HashMap<String, Object> data = new HashMap<>();
        data.put("value", value);
        return data;
    }

}
